package org.whatever.aha.zjut.platform.controller.project;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.whatever.aha.zjut.base.constant.StatusConstant;
import org.whatever.aha.zjut.base.dto.AjaxResult;
import org.whatever.aha.zjut.platform.annotation.ProjectPermission;
import org.whatever.aha.zjut.platform.annotation.StageLimit;
import org.whatever.aha.zjut.platform.dto.project.ProjectTeacherDto;
import org.whatever.aha.zjut.platform.entity.competition.CompetitionStageStatus;
import org.whatever.aha.zjut.platform.service.competition.CompetitionService;
import org.whatever.aha.zjut.platform.service.competition.CompetitionStageStatusService;
import org.whatever.aha.zjut.platform.service.project.ProjectService;
import org.whatever.aha.zjut.platform.service.project.ProjectTeacherService;
import org.whatever.aha.zjut.platform.util.CheckUtil;
import org.whatever.aha.zjut.platform.vo.project.ProjectTeacherVo;

import java.util.List;

/**
 * @author Vc, Elder Master
 * @version 1.0
 * @Desc
 * @date 2021/11/7 11:15 下午
 */

@Api(tags="项目指导老师操作类")
@RequestMapping("/v1/projectTeacher")
@RequiredArgsConstructor
@RestController
@Validated
public class ProjectTeacherController {

    final ProjectTeacherService projectTeacherService;
    final ProjectService projectService;
    final CompetitionService competitionService;
    final CompetitionStageStatusService competitionStageStatusService;

    /**
     * 根据项目id查询指导老师
     *
     * @param projectId
     * @return
     */
    @ApiOperation("根据项目id查询指导老师")
    @ApiImplicitParam(name = "projectId", value = "项目Id", dataTypeClass = Integer.class)
    @ProjectPermission
    @GetMapping("/{projectId}")
    AjaxResult<List<ProjectTeacherVo>> getProjectTeachers(@PathVariable("projectId")int projectId){
        return AjaxResult.SUCCESS(projectTeacherService.getProjectTeachers(projectId));
    }

    /**
     * 根据项目id添加指导老师
     *
     * @param projectTeacherDto
     * @return
     */
    @ApiOperation("根据项目id添加指导老师")
    @ProjectPermission
    @PostMapping("/addProjectTeacher")
    AjaxResult<Integer> addProjectTeacherById(@RequestBody ProjectTeacherDto projectTeacherDto){
        //提交阶段 && 项目未提交
        CompetitionStageStatus singleCompStageStatus = competitionStageStatusService.getSingleCompStageStatus(projectService.getProjectDetailById(projectTeacherDto.getProjectId()).getStageId(), StatusConstant.COMPETITION_STAGE_STATUS_ORDER_SUBMIT.getCode());
        CheckUtil.checkTimeLimit(singleCompStageStatus.getStatusStartTime(), singleCompStageStatus.getStatusEndTime());
        CheckUtil.checkStuStatus(StatusConstant.PROJECT_STUDENT_STATUS_WAIT_FOR_SUBMIT.getCode(), projectService.getProjectStuStatus(projectTeacherDto.getProjectId()));

        return AjaxResult.SUCCESS(projectTeacherService.addProjectTeacherById(projectTeacherDto));
    }

    @ApiOperation("根据项目id和指导老师姓名更新指导老师")
    @ProjectPermission
    @StageLimit({"submit"})
    @PutMapping("")
    AjaxResult<Object> updateProjectTeacher(@RequestBody ProjectTeacherDto projectTeacherDto){
        //提交阶段 && 项目未提交
        CompetitionStageStatus singleCompStageStatus = competitionStageStatusService.getSingleCompStageStatus(projectService.getProjectDetailById(projectTeacherDto.getProjectId()).getStageId(), StatusConstant.COMPETITION_STAGE_STATUS_ORDER_SUBMIT.getCode());
        CheckUtil.checkTimeLimit(singleCompStageStatus.getStatusStartTime(), singleCompStageStatus.getStatusEndTime());
        CheckUtil.checkStuStatus(StatusConstant.PROJECT_STUDENT_STATUS_WAIT_FOR_SUBMIT.getCode(), projectService.getProjectStuStatus(projectTeacherDto.getProjectId()));

        return AjaxResult.SUCCESS(projectTeacherService.updateProjectTeacher(projectTeacherDto));
    }


    @ApiOperation("根据项目id和指导老师排位删除指导老师记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目Id", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "teacherProjectRank", value = "老师在项目排位", dataTypeClass = Integer.class)
    })
    @ProjectPermission
    @StageLimit({"submit"})
    @DeleteMapping("")
    AjaxResult<Object> deleteProjectTeacher(@RequestParam("projectId") int projectId, @RequestParam("teacherProjectRank") int teacherProjectRank){
        return AjaxResult.SUCCESS(projectTeacherService.deleteProjectTeacher(projectId, teacherProjectRank));
    }
}